<template>
  <div class="bg-style">
    <i-pageBody class="add_i-color">
      <div slot="page-main">
        <div class="container container-space container-space--vertical">
          <div class="container-space__child">
            <Form ref="form" :model="formdata" :label-width="100">
              <Row :gutter="20">
                <i-col :span="5">
                  <FormItem prop="region" label="区域：">
                    <Cascader
                      :data="options.region"
                      v-model="formdata.region"
                      change-on-select
                    ></Cascader>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="custId" label="客户名称：">
                    <Select
                      v-model="formdata.custId"
                      placeholder="请选择客户名称"
                      style="100%"
                      clearable
                    >
                      <Option
                        :value="value"
                        v-for="({ label, value }, index) in options.cust"
                        :key="index"
                        >{{ label }}</Option
                      >
                    </Select>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="alarmId" label="告警类型：">
                    <Select
                      v-model="formdata.alarmId"
                      placeholder="请选择告警类型"
                      style="100%"
                      clearable
                    >
                      <Option
                        :value="value"
                        v-for="({ label, value }, index) in options.alarms"
                        :key="index"
                        >{{ label }}</Option
                      >
                    </Select>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="time" label="选择时间：">
                    <Date-picker
                      style="width:100%"
                      type="daterange"
                      placeholder="选择开始时间"
                      v-model="formdata.time"
                      clearable
                    ></Date-picker>
                  </FormItem>
                </i-col>
                <i-col :span="4">
                  <Button
                    type="primary"
                    :loading="loading.search"
                    @click="handleSearch"
                  >
                    查询
                  </Button>
                  <Button
                    type="default"
                    :loading="loading.reset"
                    @click="handleReset"
                  >
                    重置
                  </Button>
                </i-col>
              </Row>
            </Form>
          </div>
          <div class="container-space__child">
            <Table
              :columns="columns"
              :data="nextData"
              border
              stripe
              :loading="tableLoading"
            ></Table>
          </div>
          <div class="container-space__child" v-show="nextData.length > 0">
            <div class="container-page">
              <PageHeader
                :total="formdata.total"
                :page-size="formdata.pageSize"
                :page="formdata.page"
              ></PageHeader>
              <Page
                :total="formdata.total"
                :current="formdata.page"
                :page-size="formdata.pageSize"
                @on-change="handlePage"
                show-sizer
                @on-page-size-change="handlePageSize"
              />
            </div>
          </div>
        </div>
      </div>
    </i-pageBody>
  </div>
</template>

<script>
import PageHeader from "../components/page-header";
import { TYPE_OPTIONS, TABLE_COLUNMS } from "./constants";
import {
  getSWarningCust,
  getAlarmOrgtree,
  getAlarmType,
  getAlarmMonitor
} from "@/api/scada";
import { getTreeDataList } from "./tree";
import moment from "moment";
export default {
  name: "warning",
  props: {},
  data() {
    const columns = [...TABLE_COLUNMS];
    return {
      loading: {
        normal: false,
        search: false,
        reset: false,
        page: false
      },
      options: {
        alarms: [...TYPE_OPTIONS],
        region: [],
        cust: []
      },
      formdata: {
        region: [],
        custId: "",
        alarmId: "",
        time: [],
        page: 1,
        pageSize: 10,
        total: 0
      },
      columns,
      data: []
    };
  },
  computed: {
    tableLoading() {
      const {
        loading: { normal, search, reset, page }
      } = this;
      return normal || search || reset || page;
    },
    nextData() {
      const {
        data,
        options: { alarms }
      } = this;
      return data.map((element) => {
        const { alarmType, ...reset } = element;
        const { label } =
          alarms.find((alarm) => alarm.value === alarmType) || {};
        return {
          ...reset,
          alarmType: label
        };
      });
    }
  },
  created() {},
  mounted() {
    this.getSWarningCust();
    this.getAlarmOrgtree();
    // this.getAlarmType();
    this.getAlarmMonitor();
  },
  watch: {},
  methods: {
    handleSearch() {
      this.formdata.page = 1;
      this.getAlarmMonitor("search");
    },
    handleReset() {
      this.formdata.page = 1;
      this.$refs.form.resetFields();
      this.getAlarmMonitor("reset");
    },
    handlePage(page) {
      this.formdata.page = page;
      this.getAlarmMonitor("page");
    },
    handlePageSize(pageSize) {
      this.formdata.pageSize = pageSize;
      this.getAlarmMonitor("page");
    },
    getSWarningCust() {
      getSWarningCust().then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          this.options.cust = (result || []).map((element) => {
            const { custId, custName } = element;
            return {
              value: custId,
              label: custName
            };
          });
        }
      });
    },
    getAlarmOrgtree() {
      getAlarmOrgtree().then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          const nextResult = result ? [result] : [];
          this.options.region = getTreeDataList(nextResult);
        }
      });
    },
    getAlarmType() {
      getAlarmType().then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          this.options.alarms = (result || []).map((element) => {
            const { alarmId, alarmValue } = element;
            return {
              value: alarmId,
              label: alarmValue
            };
          });
        }
      });
    },
    getAlarmMonitor(loadingType = "normal") {
      this.loading[loadingType] = true;
      const {
        formdata: { region, custId, alarmId, time, page: currentPage, pageSize }
      } = this;
      const areaNo = region[region.length - 1] || "";
      let [startDate, endDate] = time;
      startDate = startDate ? moment(startDate).format("yyyy-MM-DD") : "";
      endDate = endDate ? moment(endDate).format("yyyy-MM-DD") : "";
      const params = {
        custId,
        alarmId,
        areaNo,
        startDate,
        endDate,
        currentPage,
        pageSize
      };
      getAlarmMonitor(params)
        .then((ret) => {
          const { code, result } = ret || {};
          if (code === 200) {
            const { totalNum, custom } = result || {};
            this.formdata.total = totalNum || 0;
            this.data = custom || [];
          }
        })
        .finally(() => {
          this.loading[loadingType] = false;
        });
    }
  },
  components: { PageHeader }
};
</script>
<style scoped lang="scss">
.bg-style {
  background: url(~@/assets/img/welcome.png) center no-repeat;
  background-size: contain;
  width: 100%;
  height: 100%;
}
.container {
  padding: 20px;
  height: 100%;
  overflow: auto;
}
.container-title {
  padding-left: 10px;
  font-size: 16px;
  color: #333;
  position: relative;
}
.container-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  display: inline-block;
  width: 4px;
  height: 16px;
  background-color: #0893fd;
  line-height: 16px;
}

.container-space > .container-space__child:not(:last-child) {
  margin-right: 20px;
}
.container-space.container-space--vertical
  > .container-space__child:not(:last-child) {
  margin-bottom: 20px;
  margin-right: 0;
}
.container-space--inner {
  padding: 20px;
}

.container-align--center {
  text-align: center;
}
.container-align--right {
  text-align: right;
}
.container-page {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
<style lang="scss" scoped>
/deep/ .ivu-table-tip table tr td {
  text-align: center;
  background-image: url("~@/assets/img/zwsj.png");
  background-position: center 60px;
  background-repeat: no-repeat;
  color: #999;
  height: 369px;
  background-size: 190px;
  span {
    display: none;
  }
}
</style>
